//
//  ProjectTableViewCell.m
//  OutesIOS
//
//  Created by norman on 2018/11/18.
//  Copyright © 2018 norman. All rights reserved.
//

#import "ProjectTableViewCell.h"
#import "UITapLabel.h"
#import "UITapImageView.h"

#define CELL_PADDING_LR 15
#define CELL_PADDING_TB 5

#define IMAGE_SIZE 24

@interface ProjectTableViewCell()

//@property (nonatomic,strong)UILabel *lbDate;
@property (nonatomic,strong)UIView *bgView;

@property (nonatomic,strong)UIImageView *ivStatus;

@property (nonatomic,strong)UILabel *lbName;

@property (nonatomic,strong)UILabel *lbArea;

@property (nonatomic,strong)UILabel *lbUser;

@property (nonatomic,strong)UILabel *lbCity;

@property (nonatomic,strong)UILabel *lbCustomer;

@property (nonatomic,strong)UITapLabel *lbTelephone;

@property (nonatomic,strong)UITapLabel *lbImages;

@property (nonatomic,strong)UITapImageView *ivImages;

@property (nonatomic,strong)UITapImageView *ivReport;

@end

@implementation ProjectTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.contentView.backgroundColor = UIColor16(0xeeeeee);

    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(CELL_PADDING_TB, CELL_PADDING_TB, kScreen_Width - 2*CELL_PADDING_TB, 0)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 4;
    //阴影效果
//    self.bgView.layer.shadowColor = UIColor16(0x000000).CGColor;
//    self.bgView.layer.shadowOffset = CGSizeMake(2, 4);
//    self.bgView.layer.shadowOpacity = 0.5;
//    self.bgView.layer.shadowRadius = 4;
    [self.contentView addSubview:self.bgView];
    
    //项目状态
    self.ivStatus = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"status_edit"]];
    self.ivStatus.x = CELL_PADDING_LR;
    [self.bgView addSubview:self.ivStatus];
    //项目名称
    self.lbName = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"默默项目名称"];
    self.lbName.textColor = CM_TABLE_TITLE_COLOR;
    self.lbName.width = self.bgView.width - 2*CELL_PADDING_LR;
    self.lbName.x = CGRectGetMaxX(self.ivStatus.frame) + 5;
    self.lbName.y = CELL_PADDING_LR;
    self.ivStatus.centerY = self.lbName.centerY;
    [self.bgView addSubview:self.lbName];
    
    UILabel *lb1 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"业主："];
    lb1.textColor = CM_TABLE_DETAIL_COLOR;
    lb1.x = CELL_PADDING_LR;
    lb1.y = CGRectGetMaxY(self.lbName.frame) + 10;
    [self.bgView addSubview:lb1];
    
    self.lbCustomer = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"张国其"];
    self.lbCustomer.textColor = CM_TABLE_TITLE_COLOR;
    self.lbCustomer.x = CGRectGetMaxX(lb1.frame) + 2;
    self.lbCustomer.y = lb1.y;
    self.lbCustomer.width = self.bgView.width/2 - self.lbCustomer.x - 5;
    [self.bgView addSubview:self.lbCustomer];
    
    UILabel *lb4 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"手机："];
    lb4.textColor = CM_TABLE_DETAIL_COLOR;
    lb4.x = self.bgView.width/2;
    lb4.y = lb1.y;
    [self.bgView addSubview:lb4];
    
    self.lbTelephone = [[UITapLabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lb4.frame) + 2, lb1.y, 0, 0)];
    self.lbTelephone.textColor = CM_BLUE_COLOR;
    self.lbTelephone.font = [UIFont systemFontOfSize:kActionBarFontSize];
    self.lbTelephone.size = [Tools sizeOfSingleLineText:@"13671036827" withSize:kActionBarFontSize];
    self.lbTelephone.width = self.bgView.width - self.lbTelephone.x - 5;
    self.lbTelephone.text = @"13671036827";
    self.lbTelephone.x = CGRectGetMaxX(lb4.frame) + 2;
    [self.bgView addSubview:self.lbTelephone];
    @weakObj(self)
    [self.lbTelephone addTargetsBlock:^(UILabel *obj) {
        if (![Tools isEmpty:obj.text]) {
            NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"tel:%@",obj.text];
            UIWebView * callWebview = [[UIWebView alloc] init];
            [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
            [Weakself.contentView addSubview:callWebview];
        }
        
    }];
    
    UILabel *lb2 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"城市："];
    lb2.textColor = CM_TABLE_DETAIL_COLOR;
    lb2.x = CELL_PADDING_LR;
    lb2.y = CGRectGetMaxY(lb1.frame) + 10;
    [self.bgView addSubview:lb2];
    
    self.lbCity = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"北京市"];
    self.lbCity.textColor = CM_TABLE_TITLE_COLOR;
    self.lbCity.x = CGRectGetMaxX(lb2.frame) + 2;
    self.lbCity.y = lb2.y;
    self.lbCity.width = self.bgView.width/2 - self.lbCity.x - 5;
    [self.bgView addSubview:self.lbCity];
    
    UILabel *lb3 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"面积："];
    lb3.textColor = CM_TABLE_DETAIL_COLOR;
    lb3.x = self.bgView.width/2;
    lb3.y = lb2.y;
    [self.bgView addSubview:lb3];
    
    self.lbArea = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"200平方米"];
    self.lbArea.textColor = CM_TABLE_TITLE_COLOR;
//    self.lbArea.width += 20;
    self.lbArea.x = CGRectGetMaxX(lb3.frame) + 2;
    self.lbArea.y = lb2.y;
    self.lbArea.width = self.bgView.width - self.lbArea.x - 5;
    [self.bgView addSubview:self.lbArea];
    
//    UILabel *lb5 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"设计师："];
//    lb5.textColor = CM_TABLE_DETAIL_COLOR;
//    lb5.x = CELL_PADDING_LR;
//    lb5.y = CGRectGetMaxY(self.lbArea.frame) + CELL_PADDING_LR;
//    [self.bgView addSubview:lb5];
//
//    self.lbUser = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"200平方米"];
//    self.lbUser.textColor = CM_TABLE_TITLE_COLOR;
//    self.lbUser.width += 20;
//    self.lbUser.x = CGRectGetMaxX(lb5.frame) + 2;
//    self.lbUser.y = lb5.y;
//    [self.bgView addSubview:self.lbUser];
//
//    UILabel *lb6 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"照片："];
//    lb6.textColor = CM_TABLE_DETAIL_COLOR;
//    lb6.x = self.bgView.width/2;
//    lb6.y = lb5.y;
//    [self.bgView addSubview:lb6];
//
//    self.lbImages = [[UITapLabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lb6.frame) + 2, self.lbUser.y, 0, 0)];
//    self.lbImages.textColor = CM_TABLE_TITLE_COLOR;
//    self.lbImages.font = [UIFont systemFontOfSize:kActionBarFontSize];
//    self.lbImages.size = [Tools sizeOfSingleLineText:@"共0张" withSize:kActionBarFontSize];
//    self.lbImages.width += 20;
//    self.lbImages.text = @"共0张";
//    [self.bgView addSubview:self.lbImages];
//    [self.lbImages addTargetsBlock:^(UILabel *obj) {
//        if (Weakself.setProjectImageCB != nil) {
//            Weakself.setProjectImageCB();
//        }
//    }];
    
    self.ivImages = [[UITapImageView alloc]initWithImage:[UIImage imageNamed:@"modify_image"]];
    self.ivImages.width = IMAGE_SIZE;
    self.ivImages.height = IMAGE_SIZE;
    [self.ivImages addTargetBlock:^(id obj) {
        if (Weakself.setProjectImageCB != nil) {
            Weakself.setProjectImageCB(1);
        }
    }];
    self.ivImages.x = self.bgView.width - 10 - self.ivImages.width;
    self.ivImages.y = 10;
    [self.bgView addSubview:self.ivImages];
    
    self.ivReport = [[UITapImageView alloc]initWithImage:[UIImage imageNamed:@"view_report"]];
    self.ivReport.width = IMAGE_SIZE;
    self.ivReport.height = IMAGE_SIZE;
    [self.ivReport addTargetBlock:^(id obj) {
        if (Weakself.setProjectImageCB != nil) {
            Weakself.setProjectImageCB(0);
        }
    }];
    self.ivReport.x = self.ivImages.x - 10 - self.ivReport.width;
    self.ivReport.y = 10;
    [self.bgView addSubview:self.ivReport];
    
    self.bgView.height = CGRectGetMaxY(self.lbArea.frame) + CELL_PADDING_LR;
    self.contentView.height = self.bgView.height + 2*CELL_PADDING_TB;
}

- (void)setProjectData:(ProjectData *)data {
    self.lbName.text = data.project_name;
    if (data.is_lock == 0) {
        self.ivStatus.image = [UIImage imageNamed:@"status_edit"];
    } else {
        self.ivStatus.image = [UIImage imageNamed:@"status_lock"];
    }
    if ([Tools isEmpty:data.report]) {
        self.ivReport.hidden = YES;
    } else {
        self.ivReport.hidden = NO;
    }
    self.lbCustomer.text = data.owner_name;
    self.lbTelephone.text = data.owner_tel;
    self.lbCity.text = data.city_name;
    self.lbArea.text = [NSString stringWithFormat:@"%d ㎡", (int)data.area];
    self.lbUser.text = data.name;
    NSInteger count = 0;
    if (![Tools isEmpty:data.images]) {
        count = [data.images componentsSeparatedByString:@","].count;
    }
    
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"共%ld张", (long)count] attributes:attribtDic];
    self.lbImages.attributedText = attribtStr;
    self.lbImages.width = [Tools sizeOfSingleLineText:self.lbImages.text withSize:kActionBarFontSize].width + 5;
//    self.ivImages.x = CGRectGetMaxX(self.lbImages.frame);
}

+ (CGFloat)cellHeight {
    CGFloat h = 2*CELL_PADDING_TB;
    h += 3*(10 + [Tools sizeOfSingleLineText:@"高度" withSize:kActionBarFontSize].height) + 2*CELL_PADDING_LR - 10;
    return h;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
